package cn.qqy.auth.controller;

import cn.qqy.auth.context.LoginContext;
import cn.qqy.auth.dto.LoginDto;
import cn.qqy.base.utils.AjaxResult;
import cn.qqy.org.domain.Employee;
import cn.qqy.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/login")
public class AuthController {

    @Autowired
    private IEmployeeService employeeService;

    @PostMapping
    public AjaxResult login(@RequestBody LoginDto loginDto){
        Employee employee = employeeService.queryByUsername(loginDto.getUsername());
        if (employee != null){
            if (employee.getPassword().equals(loginDto.getPassword())){

                String token = UUID.randomUUID().toString();//生成一个唯一数
                employee.setPassword("");//隐藏密码

                LoginContext.context.put(token,employee);
                Map<String,Object> map =new HashMap<>();
                map.put("token",token);
                map.put("user",employee);
                return AjaxResult.me().setData(map);

            }else {
                return AjaxResult.me().setSuccess(false).setMsg("用户或密码错误");
            }
        }else {
            return AjaxResult.me().setSuccess(false).setMsg("账号为空");
        }


    }
}
